<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
	<HEAD>
		<TITLE>FileHelpers Library - Marcos Meli</TITLE>
		<LINK href="/favicon.ico" rel="SHORTCUT ICON">
<link type="text/css" rel="stylesheet" href="SyntaxHighlighter.css"></link>

			<LINK href="global.css" type="text/css" rel="stylesheet">
				<meta NAME="Description" content="FileHelpers are an easy to use .NET library to read/write data from fixed length or delimited files or streams. It has support to import/export data from different data sources.">
				<meta name="Author" content="Marcos Meli">
<meta NAME="keywords" Content="filehelpers,CSV,Marcos_Meli,C#,free,opensource,Devoo.net,Delimited Files,Import/Export">
				<meta NAME="Copyright" CONTENT="Copyright 2005-07 to Marcos Meli - www.Devoo.net. All Rights Reserved.">
	</HEAD>
	<BODY text="black" vLink="purple" aLink="red" link="blue" bgColor="white" leftMargin="0"
		topMargin="0">

		{$HEADER$}


		<table width="100%" cellpadding="5" cellspacing="5">
			<tr>
				<td><div id="contentdiv">
						<h2>
                            SqlServerStorage InsertRecords Example
						</h2>
						<blockquote>
							<p>This example shows how to write records to an sql server storage</p>
						</blockquote>
						<h2>The Problem
						</h2>
						<blockquote>
							<p>For example if you want to insert records to the Customers table in the Northwind DB</p>
						</blockquote>
						<h2>
							To use Delimited Format</h2>
						<blockquote>
							<p>You must define:</p>
							<blockquote>
<textarea name="code" class="c#">

	[DelimitedRecord("|")]
	public class CustomersVerticalBar
	{
		public string CustomerID;
		public string CompanyName;
		public string ContactName;
		public string ContactTitle;
		public string Address;
		public string City;
		public string Country;
	}

</textarea>
							</blockquote></blockquote>
						<h2>
                            The SqlServerStorage</h2>
                            <blockquote>
						<p>
                            The idea is very simple you create the SqlServerStorage with your record class</p>
                    <p>
<textarea name="code" class="c#">

   SqlServerStorage storage = new SqlServerStorage(typeof(CustomersVerticalBar));
			
   storage.ServerName = "NEON-64";
   storage.DatabaseName = "Northwind";

   // Comment this for Windows Auth
   storage.UserName = "Marcos";
   storage.UserPass = "private :)";   
</textarea>
                    
                    						<p>
After these lines if you want to insert records to your db you need these lines:
<textarea name="code" class="c#">

	storage.InsertSqlCallback = new InsertSqlHandler(GetInsertSqlCust);

CustomersVerticalBar[] res = CommonEngine.ReadFile(typeof(CustomersVerticalBar), "infile.txt") as CustomerVerticalBar[];
storage.InsertRecords(res);

// or directly:

storage.InsertRecords(CommonEngine.ReadFile(typeof(CustomersVerticalBar), "infile.txt"));
   
</textarea>
                    						<p>
                                                The GetInsertSql method must look something like that:
                                                <textarea name="code" class="c#">

		protected string GetInsertSqlCust(object record)
		{
			CustomersVerticalBar obj = (CustomersVerticalBar) record;

			return String.Format("INSERT INTO Customers (Address, City, CompanyName, ContactName, ContactTitle, Country, CustomerID) " +
				" VALUES ( '{0}' , '{1}' , '{2}' , '{3}' , '{4}' , '{5}' , '{6}'  ); ",
				obj.Address,
				obj.City,
				obj.CompanyName,
				obj.ContactName,
				obj.ContactTitle,
				obj.Country,
				obj.CustomerID
				);

		}
		
</textarea>
</blockquote>                     
                    <h2>
                        Read the data from a File</h2>
                    &nbsp; &nbsp;&nbsp; You need this code for that task:<br />
                    
<blockquote>
<textarea name="code" class="c#">
    
  FileDataLink.EasyInsertFromFile(storage, "infile.txt");
  
  // or

  FileDataLink link = new FileDataLink(storage); 
  link.InsertFromToFile("infile.txt");     
</textarea>

</blockquote>
                    <br />
                    <blockquote>
		{$FOOTER$}
                    </td>
                </tr>
		</table>
	
	</BODY>
</HTML>
